<!--
 * @description  : 市管-考核计划执行列表
 * @descriptionDetail: 市管-考核计划执行列表
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-08-05 14:43:02
-->
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<view class="nav-right-icon">
				<u-icon name="list" v-if="objInfo.status != 3" color="#FFF" size="38" style="margin: 0 -14rpx 0 6rpx" @tap="selectOrg"></u-icon>
			</view>
		</theme-navbar>
		<!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		<fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->
		<view class="nolicence-search-lay">
			<u-search shape="round" :clearabled="true" :showAction="true" actionText="确定" :animation="true" placeholder="请输入企业名称" v-model="searchKey" @custom="toSearchList"></u-search>
		</view>
		<view class="content u-page">
			<me-table :i="0" :index="0" :top="102" :load-data="loadData" :height="getListViewHeight()" :init-load="false" ref="mescrollItem0">
				<view slot-scope="d" class="list-lay">
					<list-item-mpr-evaluation :itemInfo="d.data" @delHandle="delHandle" @detailHandle="detailHandle"></list-item-mpr-evaluation>
				</view>
			</me-table>
		</view>
		<view class="submit-btns btn-other-cls" v-if="objInfo.status != 3" style="padding-left:0">
			<u-button class="mit" type="primary" @click="add">
				添加
			</u-button>
		</view>
		<u-action-sheet :list="actionList" v-model="actionShow" @click="actionClick"></u-action-sheet>
		<u-popup v-model="orgShow" width="80%" mode="right">
			<view class="cearch_title">
				查询条件
			</view>
			<u-form :model="orgForm" label-position="top" label-width="auto" style="padding: 80rpx 32rpx 0 32rpx" ref="uForm">
				<u-form-item label="被考核单位:" prop="orgName" right-icon="arrow-right">
					<u-input :disabled="true" placeholder="请选择被考核单位" v-model="orgForm.orgName" @click="orgClick" />
				</u-form-item>
				<u-form-item label="考核片区:">
					<u-input :disabled="true" placeholder="选择被考核单位自动带入" type="textarea" v-model="polygonArea" />
				</u-form-item>
				<u-form-item label="经营人:" prop="managerName">
					<u-input placeholder="请输入经营人" v-model="orgForm.managerName" />
				</u-form-item>
				<u-form-item label="联系电话:" prop="retailTel">
					<u-input placeholder="请输入联系电话" v-model="orgForm.retailTel" />
				</u-form-item>
			</u-form>
			<view class="submit-btns btn-other-cls">
				<u-button plain class="del" type="primary" @click="resetForm">
					重置
				</u-button>
				<u-button class="mit" type="primary" @click="selectBtn">
					确定
				</u-button>
			</view>
		</u-popup>
		<u-select mode="single-column" :list="orgShowList" v-model="assessmentUnit" @confirm="unitConfirm"></u-select>
	</view>
</template>

<script>
	import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
	import meTable from "@/components/me-table.vue";
	import ListItemMprEvaluation from "./components/listItemMprEvaluation.vue";
	import checkCheckService from "@/service/check/check.service";
	import fabButton from '@/components/uni-fab.vue';

	export default {
		components: {
			meTable,
			fabButton,
			ListItemMprEvaluation,
		},
		mixins: [MescrollMoreMixin, ],
		data() {
			return {
				searchKey: "",
				popupHeight: "360rpx",
				swiperOptions: [{
						text: '修改',
						style: {
							backgroundColor: this.$theme.u_type_primary_disabled,
						},
					},
					{
						text: '删除',
						style: {
							backgroundColor: this.$theme.u_type_error_dark,
						},
					},
				],
				lab_style: {
					fontWeight: 500,
					color: this.$theme.u_main_color,
					fontSize: '.75rem',
				},
				actionList: [{
						"text": "添加零售户",
						"type": "retailer",
					},
					{
						"text": "添加无证户",
						"type": "nolicences",
					},
				],
				actionShow: false,
				objInfo: {},
				orgShow: false,
				orgForm: {
					orgUuid: '',
					orgName: '',
					managerName: '',
					retailTel: '',
					licName: '',
				},
				assessmentUnit: false,
				orgShowList: [],
				selectStatus: false, // 控制被考核单位
				polygonArea: "",
				orgList: [],
				areaOrgName: "",
				fContent: [{
					iconPath: require('@/static/sou.png'),
					selectedIconPath: require('@/static/sou.png'),
					text: '筛选',
					active: false,
				}],
			}
		},
		onLoad() {
			this.objInfo = this.getRouteParam();
			this.init();
			console.log(this.objInfo);
		},
		mounted() {
			this.setRightBtns();
		},
		methods: {
			setRightBtns() {
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("筛选", () => {
					this.selectOrg();
					this.setRightBtns();
				}, true, true);
				// #endif
			},
			fabtrigger(btn) {
				if (btn.index == 0) {
					this.selectOrg();
				}
			},
			init() {
				checkCheckService.PlanOrgList({
					planUuid: this.objInfo.planUuid,
				}).then(res => {
					if (res.success) {
						this.orgList = res.data;
						this.selectStatus = false;
						this.areaOrgName = res.data[0].polygonArea;
						this.areaOrgUuids = res.data[0].areaOrgUuids;
						res.data.forEach((item, index) => {
							this.orgShowList.push({
								value: item.orgUuid,
								label: item.orgName,
							});
						})
						// this.objInfo = {...this.objInfo, orgList,};
					}
				})
			},
			orgClick() {
				this.assessmentUnit = true;
			},
			resetForm() {
				this.orgForm = {
					managerName: '',
					retailTel: '',
					licName: '',
				};
			},
			unitConfirm(e) {
				this.orgForm.orgName = e[0].label
				this.orgForm.orgUuid = e[0].value
				let list = this.orgList.filter(item => {
					return item.orgUuid == this.orgForm.orgUuid;
				})
				this.polygonArea = list[0].polygonArea;
				this.areaOrgUuids = list[0].areaOrgUuids;
			},
			getListViewHeight() {
				let p = this.$u.sys().windowHeight - 44 - 48 - uni.upx2px(10) - this.$u.sys().statusBarHeight;
				// #ifdef H5-DINGDING || H5-WECHAT
				p = this.$u.sys().windowHeight - uni.upx2px(10) - 48 - this.$u.sys().statusBarHeight;
				// #endif
				return p + "px";
			},
			toSearchList(e) {
				this.getMescroll(0).triggerDownScroll();
			},
			// 用的工作待办接口筛选数据，不用做分页查询，默认多查一批数据出来
			loadData(page) {
				let param = {
					page: page.num,
					limit: page.size,
					companyName: this.searchKey,
					planUuid: this.objInfo.planUuid,
					orgUuid: this.orgForm.orgUuid,
					managerName: this.orgForm.managerName,
					retailTel: this.orgForm.retailTel,
					// licName:this.orgForm.licName,
				};
				return new Promise((resolve) => {
					checkCheckService.samplingRecordList(param).then(res => {
						if (res.success) {
							res.data.forEach(itm => {
								if (this.objInfo.status == 3) {
									itm.swiperDisabled = true;
								} else {
									itm.swiperDisabled = false;
								}
								itm.show = false;
							});
							resolve({
								count: res.count,
								data: [...res.data, ],
							});
						} else {
							resolve({
								count: 0,
								data: [],
							})
						}
					}).catch(err => {
						resolve({
							count: 0,
							data: [],
						});
					});
				})
			},
			selectOrg() {
				this.orgShow = true;
			},
			selectBtn() {
				this.orgShow = false;
				if (this.orgForm.orgUuid) {
					this.objInfo.orgUuid = this.orgForm.orgUuid;
					this.objInfo.orgName = this.orgForm.orgName;
				}
				this.selectStatus = true;
				console.log(this.objInfo)
				this.getMescroll(0).triggerDownScroll();
			},
			delHandle(id) {
				this.$modal.confirm("确定要删除吗？", () => {
					this.$modal.showLoading('删除中...');
					checkCheckService.deleteSamplingPlan({
						checkUuid: id,
					}).then(res => {
						if (res.success) {
							this.$modal.hideLoading();
							this.$u.toast('删除成功')
							this.$refs["mescrollItem0"].refresh();
						}
					}).catch(err => {
						this.$modal.hideLoading();
						this.$u.toast('删除失败')
					})
				})
			},
			detailHandle(info) {
				this.setRouteParam(info);
				this.$u.route({
					url: "/pages/mprEvaluation/mprEvaluationCheck",
					params: {
						from: 'edit',
					},
				});
			},
			actionClick(index) {
				this.actionShow = false;
				let param = {
					orgUuid: this.orgShowList[0].value,
					orgName: this.orgShowList[0].label,
				}
				if (this.$u.test.isEmpty(this.polygonArea)) {
					this.$set(this.objInfo, 'polygonArea', this.areaOrgName);
				} else {
					this.$set(this.objInfo, 'polygonArea', this.polygonArea);
				}
				// this.$set(this.objInfo, 'polygonArea', this.polygonArea);
				this.$set(this.objInfo, 'areaOrgUuids', this.areaOrgUuids);
				if (this.selectStatus) {
					this.setRouteParam(this.objInfo);
				} else {
					if (this.orgShowList[0].value) {
						this.setRouteParam({
							...this.objInfo,
							...param,
						});
					} else {
						this.setRouteParam(this.objInfo);
					}
				}
				console.log(this.objInfo)
				this.$u.route("pages/mprEvaluation/mprChoiceList?type=" + this.actionList[index].type);
			},
			add() {
				this.actionShow = true;
			},
			updateBackState(param) {
				if (param.refresh.productUuid) {
					this.getMescroll(0).triggerDownScroll();
				} else {
					this.getMescroll(0).triggerDownScroll();
				}
			},
			swiperOpen(index) {
				this.$refs.mescrollItem0.listDatas.map((val, idx) => {
					this.$refs.mescrollItem0.listDatas[idx].show = index == val.checkUuid;
				});
			},
		},
	}
</script>

<style scoped lang="scss">
	@import "@/style/common/rightMenu.scss";

	.page {
		padding: 0;
	}

	.content {
		background-color: #F3F4F5;
		min-height: 100%;
	}

	.nolicence-search-lay {
		background-color: #fff;
		position: fixed;
		// #ifndef H5-DINGDING
		top: 44px;
		// #endif
		// #ifdef H5-DINGDING
		top: 0px;
		// #endif
		z-index: 999;
		width: 100%;
		overflow: hidden;
		padding: $cm-search-padding-row $cm-search-padding-column;
	}

	.list-item-root {
		border-bottom: 1px solid $u-border-color;
	}

	.list-lay {
		margin: $cm-conent-padding-column $cm-content-paddng-row;
	}

	.btn {
		position: fixed;
		bottom: 50rpx;
		margin: auto;
		left: 60rpx;
		right: 60rpx;
		z-index: 100;
		background-color: #ffffff;
	}

	.cearch_title {
		background-color: #F3F4F5;
		width: 100%;
		height: 82rpx;
		font-size: 32rpx;
		font-weight: 600;
		padding: 22rpx 0 21rpx 32rpx;
		z-index: 999;
		position: fixed;
	}
</style>
